package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"logi-flow/pkg"
	"strconv"
	"time"
)

func Ass(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "字段不能为空",
		})
		return
	}

	redis_add_key := "add_key" + username
	errcount, _ := cache.Rdb.Get(cache.Ctx, redis_add_key).Int()
	if errcount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "密码错误超出三次，禁止登录",
		})
		return
	}
	var data model.User

	if err := model.DB.Where("username = ?", username).Find(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "账号或密码错误",
		})
		return
	}

	if data.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "账号或密码错误",
		})
		return
	}

	if data.Password != pkg.MD5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_add_key)
		if errcount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_add_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"smg":  "密码错误超过三次，请两个小时后再试",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "密码错误",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "登录成功",
		"data": data,
	})
	return
}
func Bss(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"smg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "添加成功",
	})
	return
}
func Css(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"smg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "添加成功",
	})
	return
}
func Dss(c *gin.Context) {
	uid := c.PostForm("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		  WHERE user_roles.uid = 1
	*/
	var data model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		First(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "查询成功",
		"data": data,
	})
	return

}
func Ess(c *gin.Context) {
	rid := c.PostForm("rid")

	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE role_nodes.rid = 1
	*/
	var data model.Node

	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid = ?", rid).
		Debug().
		Find(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "查询成功",
		"data": data,
	})
	return

}

func Tss(c *gin.Context) {
	page, _ := strconv.Atoi(c.PostForm("page"))
	size, _ := strconv.Atoi(c.PostForm("size"))
	offset := (page - 1) * size

	var data []model.DingList
	/*
		SELECT
		dings.id AS Id,
		dings.ding_bian AS ListBian,
		dings.kei AS ListKei,
		dings.dizhi AS ListDizhi,
		peis.pname AS ListPname
		FROM
		dings
		LEFT JOIN peis ON dings.pid = peis.id
	*/

	redis_list_key := "list_key" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()
	if result == "" {
		model.DB.Table("dings").
			Select(
				"dings.id AS Id",
				"dings.ding_bian AS ListBian",
				"dings.kei AS ListKei",
				"dings.dizhi AS ListDizhi",
				"peis.pname AS ListPname",
			).
			Joins("LEFT JOIN peis ON dings.pid = peis.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	var total int64
	model.DB.Model(&model.Ding{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"smg":   "查询成功",
		"data":  data,
		"total": total,
	})
	return
}
